<?php
/**
 * 日记
 * @author  zhanghui
 * @create_date 2008-02-26
 */

class Diary{
    function __construct(){}
    
    function __destruct(){}
    
    /* 增加日记  
     * @param : $arr_input -  baby_id,folderid,postid,read_open_type,read_password,title,content,ip_address,diary_date,weatherid
                              ,emoticonid,create_date,update_date
     */
    public function addDiary($conn,$arr_input){
        $str_query = "insert into";
        $str_query .= " baby_diary";
        $str_query .= " set";
        $str_query .= " baby_id={$arr_input['baby_id']}";
        $str_query .= ",folderid={$arr_input['folderid']}";
        $str_query .= ",read_open_type={$arr_input['read_open_type']}";
        $str_query .= ",read_password='{$arr_input['read_password']}'";
        $str_query .= ",title='{$arr_input['title']}'";
        $str_query .= ",content='{$arr_input['content']}'";
        $str_query .= ",ip_address='{$arr_input['ip_address']}'";
        $str_query .= ",diary_date='{$arr_input['diary_date']}'";
        $str_query .= ",weatherid={$arr_input['weatherid']}";
        $str_query .= ",emoticonid={$arr_input['emoticonid']}";
        $str_query .= ",create_date='".$conn->getCurrentTime()."'";
        
        $conn->executeQuery($str_query);
        
        $obj_DirayCnt = new DiaryCnt();
        $obj_DirayCnt->initDiaryCnt($conn,$arr_input);
    }
    
    /* 日记编辑
     *@param : $arr_input -  baby_id,folderid,postid,read_open_type,read_password,title,content,ip_address,diary_date,weatherid
                              ,emoticonid,update_date
    */
    public function editDiary($conn,$arr_input){
    	$str_query = "update";
    	$str_query .= " baby_diary";
    	$str_query .= " set";
        $str_query .= " read_open_type={$arr_input['read_open_type']}";
        $str_query .= ",read_password='{$arr_input['read_password']}'";
        $str_query .= ",title='{$arr_input['title']}'";
        $str_query .= ",content='{$arr_input['content']}'";
        $str_query .= ",ip_address='{$arr_input['ip_address']}'";
        $str_query .= ",diary_date='{$arr_input['diary_date']}'";
        $str_query .= ",weatherid={$arr_input['weatherid']}";
        $str_query .= ",emoticonid={$arr_input['emoticonid']}";
        $str_query .= ",update_date='".$conn->getCurrentTime()."'";
        $str_query .= " where";
        $str_query .= " baby_id = {$arr_input['baby_id']}";
        $str_query .= " folderid = {$arr_input['folderid']}";
        $str_query .= " and postid = {$arr_input['postid']}";
        
        $conn->executeQuery($str_query);
    }
    
    /* 删除日记
     * @param : $arr_input -  baby_id,folderid,postid
    */
    public function deleteDiary($conn,$arr_input){
    	$str_query = "delete from";
    	$str_query .= " baby_diary";
    	$str_query .= " where";
    	$str_query .= " baby_id={$arr_input['baby_id']}";
    	$str_query .= " and folderid={$arr_input['folderid']}";
    	$str_query .= " and postid={$arr_input['postid']}";
    	
    	$conn->executeQuery($str_query);
    	
    	$obj_DiaryComment = new DiaryComment();
    	$obj_DiaryComment->deleteComment($conn,$arr_input);
    }
    
    /* 获取日记
     * @param : $arr_input -  baby_id,folderid,postid,read_open_type,read_password,title,content,ip_address,diary_date,weatherid
                              ,emoticonid,create_date,update_date
     * @return : $arr_output 
     *           baby_id,             [0]<br>
     *		     folderid,            [1]<br>
     *           postid,              [2]<br>
     *		     read_open_type,      [3]<br>
     *           read_password,       [4]<br>
     *           title,               [5]<br>
     *           content,             [6]<br>
     *           ip_address,          [7]<br>
     *           diary_date,          [8]<br>
     *           weatherid,           [9]<br>
     *           emoticonid,          [10]<br>
     *           create_date,         [11]<br>
     *           update_date,         [12]<br>
    */
    public function getDiaryList($conn,$arr_input,$limit = null){
    	$str_query = "select";
    	if(isset($arr_input['cnt'])){
    		$str_query .= " count(1)";
    	}else{
    		$str_query .= " baby_id";
    		$str_query .= " ,folderid";
    		$str_query .= " ,postid";
    		$str_query .= " ,read_open_type";
    		$str_query .= " ,read_password";
    		$str_query .= " ,title";
    		$str_query .= " ,content";
    		$str_query .= " ,ip_address";
    		$str_query .= " ,diary_date";
    		$str_query .= " ,weatherid";
    		$str_query .= " ,emoticonid";
    		$str_query .= " ,create_date";
    		$str_query .= " ,update_date";
    	}
    	$str_query .=" from";
    	$str_query .=" baby_diary";
    	$str_query .=" where";
    	$str_query .=" baby_id={$arr_input['baby_id']}";
    	
    	if(isset($arr_input['folderid'])){
    		$str_query .=" and folderid={$arr_input['folderid']}";
    	}
    	if(isset($arr_input['postid'])){
    		$str_query .=" and postid={$arr_input['postid']}";
    	}
    	if (isset($arr_input['orderby'])) {
    		$str_query .= " order by {$arr_input['orderby']}";
    	}
    	if ($limit != null){
    		$str_query .= " {$limit}";
    	}

    	$arr_output = $conn->selectQuery($str_query);
    	return $arr_output;
    }
}
?>